#pragma once

#include <pthread.h>
#include <sys/syscall.h>

#include "noncopyable.h"

namespace utils
{
class MutexLock : noncopyable
{
    private:
        pthread_mutex_t mutex;
        pid_t threadID;
    public:
        MutexLock()
        {
            pthread_mutex_init(&mutex, nullptr);
        }
        void Lock()
        {
            pthread_mutex_lock(&mutex);
            threadID = ::pthread_self();
        }
        void UnLock()
        {
            threadID = 0;
            pthread_mutex_unlock(&mutex);
        }
        pthread_mutex_t* GetPthreadMutex()
        {
            return &mutex;
        }
};

class MutexLockGuard
{
    private:
        MutexLock& mutex;
    public:
        explicit MutexLockGuard(MutexLock& mutex_) : mutex(mutex_)
        {
            mutex.Lock();
        }
        ~MutexLockGuard()
        {
            mutex.UnLock();
        }
};
}
